package star.cloud.asme.base.dto;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

@SuppressWarnings("ALL")
@Service
public class ResponseService {

    public void generateResponse(HttpServletResponse response, Map<Object, Object> map) throws IOException {
        String jsonMap = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.setHeader("result", "ok");
        response.getWriter().println(jsonMap);
    }

    public void generateResponse(HttpServletResponse response, Map<Object, Object> map, String result) throws IOException {
        if (Objects.isNull(result) || !result.equals("error")) {
            result = "ok";
        }
        String jsonMap = new ObjectMapper().writeValueAsString(map);
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.setHeader("result", result);
        response.getWriter().println(jsonMap);
    }
}